package com.wss.common.widget;

import com.orhanobut.logger.Logger;
import com.wss.common.utils.ColorUtils;
import com.wss.common.utils.TextUtils;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ObserverButton extends Text {

    private List<TextField> editTextList = new ArrayList<>();

    private boolean canPress;
    private int defaultBg = Color.GRAY.getValue();
    private int pressBg = Color.BLUE.getValue();
    private int defaultTextColor = Color.WHITE.getValue();
    private int pressTextColor = Color.WHITE.getValue();
    private Element defaultBgRes;
    private Element pressBgRes;

    public ObserverButton(Context context) {
        this(context, null);
    }

    public ObserverButton(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public ObserverButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        defaultBg = attrSet.getAttr("defaultBgColor").isPresent() ?
                attrSet.getAttr("defaultBgColor")
                        .get()
                        .getColorValue()
                        .getValue() : defaultBg;
        pressBg = attrSet.getAttr("pressBgColor").isPresent() ?
                attrSet.getAttr("pressBgColor")
                        .get()
                        .getColorValue()
                        .getValue() : pressBg;
        defaultTextColor = attrSet.getAttr("defaultTextColor").isPresent() ?
                attrSet.getAttr("defaultTextColor")
                        .get()
                        .getColorValue()
                        .getValue() : defaultTextColor;
        pressTextColor = attrSet.getAttr("pressTextColor").isPresent() ?
                attrSet.getAttr("pressTextColor")
                        .get()
                        .getColorValue()
                        .getValue() : pressTextColor;

        if (attrSet.getAttr("defaultBgResource").isPresent()) {
            defaultBgRes = attrSet.getAttr("defaultBgResource").get().getElement();
        }

        if (attrSet.getAttr("pressBgResource").isPresent()) {
            pressBgRes = attrSet.getAttr("pressBgResource").get().getElement();
        }

        initBtn();
    }

    public void observer(@NotNull TextField... textFields) {
        for (TextField textField : textFields) {
            textField.addTextObserver(textObserver);
            editTextList.add(textField);
        }
    }

    private void initBtn() {

        if (canPress) {
            setTextColor(new Color(pressTextColor));
            ShapeElement color = new ShapeElement();
            color.setRgbColor(ColorUtils.colorToRgbColor(pressBg));
            setBackground(color);
            if (pressBgRes != null) {
                setBackground(pressBgRes);
            }
            setClickable(true);
            setEnabled(true);
        } else {
            setTextColor(new Color(defaultTextColor));
            ShapeElement color = new ShapeElement();
            color.setRgbColor(ColorUtils.colorToRgbColor(defaultBg));
            setBackground(color);
            if (defaultBgRes != null) {
                setBackground(defaultBgRes);
            }
            setClickable(false);
            setEnabled(false);
        }
    }

    public void setPressEnable(boolean canPress) {
        this.canPress = canPress;
        initBtn();
    }

    private TextObserver textObserver = new TextObserver() {
        @Override
        public void onTextUpdated(String s, int i, int i1, int i2) {
            checkEditText();
        }
    };

    /**
     * 检查输入框输入
     */
    private void checkEditText() {
        canPress = true;
        for (TextField et : editTextList) {

            if (TextUtils.isEmpty(et.getText().trim())) {
                canPress = false;
                break;
            }
        }
        initBtn();
    }
}
